﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;

namespace smartphone.App_Code
{
    public class Catalog
    {

        /// <summary>
        /// Lay danh sach cac loai thuoc tinh
        /// </summary>
        /// <returns></returns>
        public DataTable GetAttrType()
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "GetAttributeType";

            DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
            return table;
        }


        /// <summary>
        /// Cap nhat loai thuoc tinh
        /// </summary>
        /// <param name="maThuocTinh"></param>
        /// <param name="tenThuocTinh"></param>
        /// <returns></returns>
        public bool UpdateAttrType(int maThuocTinh, string tenThuocTinh)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "UpdateAttributeType";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maThuocTinh";
            param.Value = maThuocTinh;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@tenThuocTinh";
            param.Value = tenThuocTinh;
            param.DbType = DbType.String;
            comm.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(comm);

            return (result != -1);
        }


        /// <summary>
        /// Tao 1 loai thuoc tinh moi
        /// </summary>
        /// <param name="tenThuocTinh"></param>
        /// <returns></returns>
        public bool CreateAttr(string tenThuocTinh)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "CreateAttributeType";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@tenThuocTinh";
            param.Value = tenThuocTinh;
            param.DbType = DbType.String;
            comm.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(comm);

            return (result != -1);
        }

        /// <summary>
        /// Xoa loai thuoc tinh
        /// </summary>
        /// <param name="maThuocTinh"></param>
        /// <returns></returns>
        public bool DeleteAttr(int maThuocTinh)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "DeleteAttributeType";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maThuocTinh";
            param.Value = maThuocTinh;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(comm);

            return (result != -1);
        }

        /// <summary>
        /// Lay cac gia tri cua 1 loai thuoc tinh
        /// </summary>
        /// <param name="maThuocTinh">Ma loai thuoc tinh can lay cac gia tri</param>
        /// <returns></returns>
        public DataTable GetAttrValue(int maThuocTinh)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "GetAttrValue";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maThuocTinh";
            param.Value = maThuocTinh;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
            return table;
        }

        /// <summary>
        /// Tao gia tri cho 1 thuoc tinh
        /// </summary>
        /// <param name="maThuocTinh"></param>
        /// <param name="giaTriThuocTinh"></param>
        /// <returns></returns>
        public bool CreateAttrValue(int maThuocTinh, string giaTriThuocTinh)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "CreateAttrValue";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maThuocTinh";
            param.Value = maThuocTinh;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@giaTriThuocTinh";
            param.Value = giaTriThuocTinh;
            param.DbType = DbType.String;
            comm.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(comm);
            return (result != -1);
        }


        /// <summary>
        /// Cap nhat gia tri thuoc tinh
        /// </summary>
        /// <param name="maGiaTriThuocTinh"></param>
        /// <param name="giaTriThuocTinh"></param>
        /// <returns></returns>
        public bool UpdateAttrValue(int maGiaTriThuocTinh, string giaTriThuocTinh)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "UpdateAttrValue";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maGiaTriThuocTinh";
            param.Value = maGiaTriThuocTinh;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@giaTriThuocTinh";
            param.Value = giaTriThuocTinh;
            param.DbType = DbType.String;
            comm.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(comm);
            return (result != -1);
        }

        /// <summary>
        /// Xoa 1 gia tri thuoc tinh
        /// </summary>
        /// <param name="maGiaTriThuocTinh"></param>
        /// <returns></returns>
        public bool DeleteAttrValue(int maGiaTriThuocTinh)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "DeleteAttrValue";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maGiaTriThuocTinh";
            param.Value = maGiaTriThuocTinh;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(comm);
            return (result != -1);
        }

        /// <summary>
        /// Lay ten cua loai thuoc tinh hien dang duoc sua
        /// </summary>
        /// <param name="maThuocTinh"></param>
        /// <returns></returns>
        public DataTable GetCurrentAttr(int maThuocTinh)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "GetSingleAttr";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maThuocTinh";
            param.Value = maThuocTinh;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
            return table;
        }

    }
}